<template>
	<div class="personal">
		<el-row>
			<!-- 个人信息 -->
			<el-col :xs="24" :sm="16">
				<el-card shadow="hover" header="个人信息">
					<div class="personal-user">
						<div class="personal-user-left">
							<el-upload class="h100 personal-user-left-upload" action="https://www.mocky.io/v2/5185415ba171ea3a00704eed/posts/" multiple :limit="1">
								<img src="\@\assets\images\avatar.jpg" v-if = "getUserInfos.userName === 'admin_HR'">
								<img src="\@\assets\images\avatar1.jpg" v-if = "getUserInfos.userName === 'super_admin'">
								<img src="\@\assets\images\avatar2.jpg" v-if = "getUserInfos.userName === 'teamleader'">
								<img src="\@\assets\images\avatar3.jpg" v-if = "getUserInfos.userName === 'admin'">
								<img src="\@\assets\images\avatar4.jpg" v-if = "getUserInfos.userName === 'chairperson'">
								<img src="\@\assets\images\avatar5.jpg" v-if = "getUserInfos.userName === 'staff1'">
							</el-upload>
						</div>
						<div class="personal-user-right">
							<el-row>
								<el-col :span="24" class="personal-title mb18">
									{{ currentTime }}，
									<span v-if="getUserInfos.userName === 'admin_HR'">小X</span>
									<span v-if="getUserInfos.userName === 'super_admin'">超级管理员</span>
									<span v-if="getUserInfos.userName === 'admin'">管理员</span>
									<span v-if="getUserInfos.userName === 'teamleader'">小Y</span>
									<span v-if="getUserInfos.userName === 'chairperson'">小M</span>
									<span v-if="getUserInfos.userName === 'staff1'">小N</span>
									<span v-if="getUserInfos.userName === ''">普通员工</span>
									，
									<span>{{autograph}}</span> </el-col>
								<el-col :span="24">
									<el-row>
										<el-col :xs="24" :sm="8" class="personal-item mb6">
											<div class="personal-item-label">昵称：</div>
											<div class="personal-item-value">
												<span v-if="getUserInfos.userName === 'admin_HR'">小X</span>
												<span v-if="getUserInfos.userName === 'super_admin'">超级管理员</span>
												<span v-if="getUserInfos.userName === 'admin'">管理员</span>
												<span v-if="getUserInfos.userName === 'teamleader'">小Y</span>
												<span v-if="getUserInfos.userName === 'chairperson'">小M</span>
												<span v-if="getUserInfos.userName === 'staff1'">小N</span>
												<span v-if="getUserInfos.userName === ''">普通员工</span>
											</div>
										</el-col>
										<el-col :xs="24" :sm="16" class="personal-item mb6">
											<div class="personal-item-label">性别：</div>
											<div class="personal-item-value">
												{{sex}}
											</div>
										</el-col>
									</el-row>
								</el-col>
								<el-col :span="24">
									<el-row>
										<el-col :xs="24" :sm="8" class="personal-item mb6">
											<div class="personal-item-label">登录IP：</div>
											<div class="personal-item-value">192.168.1.1</div>
										</el-col>
										<el-col :xs="24" :sm="16" class="personal-item mb6">
											<div class="personal-item-label">登录时间：</div>
											<div class="personal-item-value">{{txt}}</div>
										</el-col>
									</el-row>
								</el-col>
							</el-row>
						</div>
					</div>
				</el-card>
			</el-col>

			<!-- 消息通知 -->
			<el-col :xs="24" :sm="8" class="pl15 personal-info">
				<el-card shadow="hover">
					<template #header>
						<span>消息通知 <el-badge value="hot" class="item"></el-badge></span>
						<span class="personal-info-more">更多</span>
					</template>
					<div class="personal-info-box">
						<ul class="personal-info-ul">
							<li v-for="(v, k) in newsInfoList" :key="k" class="personal-info-li">
								<a :href="v.link" target="_block" class="personal-info-li-title">
									<el-badge value="new" class="item"></el-badge>
									{{ v.title }}
								</a>
							</li>
						</ul>
					</div>
				</el-card>
			</el-col>

			<!-- 更新信息 -->
			<el-col :span="24">
				<el-card shadow="hover" class="mt15 personal-edit" header="更新信息">
					<div class="personal-edit-title">基本信息</div>
					<el-form :model="personalForm" size="small" label-width="40px" class="mt35 mb35">
						<el-row :gutter="35">
							<el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="6" class="mb20">
								<el-form-item label="昵称">
									<el-input v-model="personalForm.name" placeholder="请输入昵称" clearable></el-input>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="6" class="mb20">
								<el-form-item label="性别">
									<el-select v-model="personalForm.sex" placeholder="请选择性别" clearable class="w100">
										<el-option label="外星人" value="外星人"></el-option>
										<el-option label="男" value="男"></el-option>
										<el-option label="女" value="女"></el-option>
									</el-select>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="6" class="mb20">
								<el-form-item label="签名">
									<el-input v-model="personalForm.autograph" placeholder="请输入签名" clearable></el-input>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="6" class="mb20">
								<el-form-item label="手机">
									<el-input v-model="personalForm.phone" placeholder="请输入手机" clearable></el-input>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="6" class="mb20">
								<el-form-item label="邮箱">
									<el-input v-model="personalForm.email" placeholder="请输入邮箱" clearable></el-input>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="6" class="mb20">
								<el-form-item label="微信">
									<el-input v-model="personalForm.wx" placeholder="请输入微信" clearable></el-input>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
								<el-form-item align='center'>
									<el-button type="primary" icon="el-icon-position" @click='updateAutograph'>更新个人信息</el-button>
								</el-form-item>
							</el-col>
						</el-row>
					</el-form>
					<div class="personal-edit-title mb15">账号安全</div>
					<div class="personal-edit-safe-box">
						<div class="personal-edit-safe-item">
							<div class="personal-edit-safe-item-left">
								<div class="personal-edit-safe-item-left-label">账户密码</div>
								<div class="personal-edit-safe-item-left-value">当前密码强度：强</div>
							</div>
							<div class="personal-edit-safe-item-right">
								<el-button type="text" @click="dialogFormVisible = true">立即修改</el-button>
							</div>
						</div>
					</div>
					<div class="personal-edit-safe-box">
						<div class="personal-edit-safe-item">
							<div class="personal-edit-safe-item-left">
								<div class="personal-edit-safe-item-left-label">密保手机</div>
								<div class="personal-edit-safe-item-left-value">已绑定手机：{{phone}}</div>
							</div>
							<div class="personal-edit-safe-item-right">
								<el-button type="text">立即修改</el-button>
							</div>
						</div>
					</div>
					<div class="personal-edit-safe-box">
						<div class="personal-edit-safe-item">
							<div class="personal-edit-safe-item-left">
								<div class="personal-edit-safe-item-left-label">绑定邮箱</div>
								<div class="personal-edit-safe-item-left-value">已绑定邮箱：{{email}}</div>
							</div>
							<div class="personal-edit-safe-item-right">
								<el-button type="text">立即设置</el-button>
							</div>
						</div>
					</div>
					<div class="personal-edit-safe-box">
						<div class="personal-edit-safe-item">
							<div class="personal-edit-safe-item-left">
								<div class="personal-edit-safe-item-left-label">绑定微信</div>
								<div class="personal-edit-safe-item-left-value">已绑定微信：{{wx}}</div>
							</div>
							<div class="personal-edit-safe-item-right">
								<el-button type="text">立即设置</el-button>
							</div>
						</div>
					</div>
				</el-card>
			</el-col>
		</el-row>


		<el-dialog title="修改密码" width="40%" center v-model="dialogFormVisible">
			<el-form :model="form">
				<el-form-item label="输入原密码" :label-width="formLabelWidth">
					<el-input v-model="form.oldpw" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="输入新密码" :label-width="formLabelWidth">
					<el-input v-model="form.newpw" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="再输入新密码" :label-width="formLabelWidth">
					<el-input v-model="form.newpw2" autocomplete="off"></el-input>
				</el-form-item>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="dialogFormVisible = false">取 消</el-button>
					<el-button type="primary" @click="updatePassword">修 改</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, computed, onBeforeMount, onUnmounted,defineComponent } from 'vue';
import { ElMessage } from 'element-plus'
import { formatAxis } from '/@/utils/formatTime.ts';
import { formatDate } from '/@/utils/formatTime.ts';
import { newsInfoList, recommendList } from './mock.ts';
import { useStore } from '/@/store/index.ts';
export default {
	name: 'personal',
	setup() {
		const store = useStore();
		const state = reactive({
			newsInfoList,
			recommendList,
			personalForm: {
				name: '',
				email: '',
				autograph: '',
				phone: '',
				sex: '',
			},
			phone:'111****1111',
			email:'XXXX163@.com',
			wx:'111****1111',
			autograph: '生活变的再糟糕，也不妨碍我变得更好！',
			txt: '',
			sex:'男',
		});
		// 获取用户信息 vuex
		const getUserInfos = computed(() => {
			return store.state.userInfos.userInfos;
		});
		// 当前时间提示语
		const currentTime = computed(() => {
			return formatAxis(new Date());
		});

		// 初始化时间
		const initTime2 = () => {
			state.txt = formatDate(new Date(), 'YYYY-mm-dd HH:MM:SS');
		};

		const updateAutograph = () =>{
			if(state.personalForm.name != ''){
				state.name = state.personalForm.name;
				ElMessage.success({
					message: '更新成功！',
					type: 'success'
				});
			};
			if(state.personalForm.email != ''){
				state.email = state.personalForm.email;
				ElMessage.success({
					message: '更新成功！',
					type: 'success'
				});
			}
			if(state.personalForm.autograph != ''){
				state.autograph = state.personalForm.autograph;
				ElMessage.success({
					message: '更新成功！',
					type: 'success'
				});
			}
			if(state.personalForm.phone != ''){
				state.phone = state.personalForm.phone;
				ElMessage.success({
					message: '更新成功！',
					type: 'success'
				});
			}
			if(state.personalForm.sex != ''){
				state.sex = state.personalForm.sex;
				ElMessage.success({
					message: '更新成功！',
					type: 'success'
				});
			}
		};

		// 页面加载前
		onBeforeMount(() => {
			initTime2();
		});
		// 页面卸载时
		onUnmounted(() => {
			window.clearInterval(state.time.fun);
		});

		return {
			getUserInfos,
			currentTime,
			updateAutograph,
			...toRefs(state),
		};
	},
	data() {
		return {
			dialogFormVisible: false,
			form: {
				oldpw: '',
				newpw: '',
				newpw2: '',
				date2: '',
				delivery: false,
				type: [],
				resource: '',
				desc: ''
			},
			formLabelWidth: '120px'
		}
	},
	methods: {
		updatePassword(){
			this.dialogFormVisible = false
			ElMessage.success({
				message: '修改成功！',
				type: 'success'
			});
		}
	},
};
</script>

<style scoped lang="scss">
@import '../../theme/mixins/mixins.scss';
.personal {
	.personal-user {
		height: 130px;
		display: flex;
		align-items: center;
		.personal-user-left {
			width: 100px;
			height: 130px;
			border-radius: 3px;
			::v-deep(.el-upload) {
				height: 100%;
			}
			.personal-user-left-upload {
				img {
					width: 100%;
					height: 100%;
					border-radius: 3px;
				}
				&:hover {
					img {
						animation: logoAnimation 0.3s ease-in-out;
					}
				}
			}
		}
		.personal-user-right {
			flex: 1;
			padding: 0 15px;
			.personal-title {
				font-size: 18px;
				@include text-ellipsis(1);
			}
			.personal-item {
				display: flex;
				align-items: center;
				font-size: 13px;
				.personal-item-label {
					color: gray;
					@include text-ellipsis(1);
				}
				.personal-item-value {
					@include text-ellipsis(1);
				}
			}
		}
	}
	.personal-info {
		.personal-info-more {
			float: right;
			color: gray;
			font-size: 13px;
			&:hover {
				color: var(--color-primary);
				cursor: pointer;
			}
		}
		.personal-info-box {
			height: 130px;
			overflow: hidden;
			.personal-info-ul {
				list-style: none;
				.personal-info-li {
					font-size: 13px;
					padding-bottom: 10px;
					.personal-info-li-title {
						display: inline-block;
						@include text-ellipsis(1);
						color: grey;
						text-decoration: none;
					}
					& a:hover {
						color: var(--color-primary);
						cursor: pointer;
					}
				}
			}
		}
	}
	.personal-recommend-row {
		.personal-recommend-col {
			.personal-recommend {
				position: relative;
				height: 100px;
				color: #ffffff;
				border-radius: 3px;
				overflow: hidden;
				cursor: pointer;
				&:hover {
					i {
						right: 0px !important;
						bottom: 0px !important;
						transition: all ease 0.3s;
					}
				}
				i {
					position: absolute;
					right: -10px;
					bottom: -10px;
					font-size: 70px;
					transform: rotate(-30deg);
					transition: all ease 0.3s;
				}
				.personal-recommend-auto {
					padding: 15px;
					position: absolute;
					left: 0;
					top: 5%;
					.personal-recommend-msg {
						font-size: 12px;
						margin-top: 10px;
					}
				}
			}
		}
	}
	.personal-edit {
		.personal-edit-title {
			position: relative;
			padding-left: 10px;
			color: #606266;
			&::after {
				content: '';
				width: 2px;
				height: 10px;
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				background: var(--color-primary);
			}
		}
		.personal-edit-safe-box {
			border-bottom: 1px solid #ebeef5;
			padding: 15px 0;
			.personal-edit-safe-item {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.personal-edit-safe-item-left {
					flex: 1;
					overflow: hidden;
					.personal-edit-safe-item-left-label {
						color: #606266;
						margin-bottom: 5px;
					}
					.personal-edit-safe-item-left-value {
						color: gray;
						@include text-ellipsis(1);
						margin-right: 15px;
					}
				}
			}
			&:last-of-type {
				padding-bottom: 0;
				border-bottom: none;
			}
		}
	}
}
</style>
